<?php
 
 class __kernel_remote_api {

 	function returnData() {

 		if($_SERVER['HTTP_REMOTE_LOGIN'] != "guest") {
 			if($_SESSION['user']['access'] >= DEFAULT_USER_ACCESS) {
 				if(__module_user_api::login($_SERVER['HTTP_REMOTE_LOGIN'], $_SERVER['HTTP_REMOTE_PASS'])) {
 					header('remote_login: ok');
 				} else {
 					header('remote_login: false');
 					return;
 				}
 			}
 		}

 		echo  "<!-- SERVER RESPONSE : BEGIN -->\n";
 		//print_r($_SESSION);
 		$class = '__module_'.$_SERVER['HTTP_REMOTE_CLASS'].'_remoteMap';
 			
 		if(class_exists($class, true)) {
 				
 			$r = new $class();
 				
 			// Check access
 			$permissions = false;
 			
 			if(is_array($r->remote[$_SERVER['HTTP_REMOTE_METHOD']])) {
 				foreach($r->remote[$_SERVER['HTTP_REMOTE_METHOD']] as $key=>$item) {
 					if($key == "<=" && $_SESSION['user']['access'] <= $item) $permissions = true;
 					if($key == ">=" && $_SESSION['user']['access'] >= $item) $permissions = true;
 					if($key == "==" && $_SESSION['user']['access'] == $item) $permissions = true;
 				}
 			} else $permissions = true;
 			
 			if($permissions == true) {
	 			$return = $r->$_SERVER['HTTP_REMOTE_METHOD'](unserialize($_SERVER['HTTP_REMOTE_ARGS']));
	 			
	 			echo serialize($return);
 			} else {
 				echo 'Permission deny';
 			}
 			
 		} else {
 			echo "Class '{$class}' not found - 404";
 		}
 		
 		echo "\n<!-- SERVER RESPONSE : END -->";
 		return;
 		
 	}
 	
  	function getData(KT_remote_server_getData $args) {

  		//__kernel_system_debug::clean('remote');
  		__kernel_system_debug::s('remote', 'host:'.$args->getVar('host'));
  		
  		$arr = array();
  		
  		$fp = fsockopen($args->getVar('host'), $args->getVar('port'), $err['no'], $err['str']);
  		
  		if(!$fp) {
  			__kernel_system_debug::s('remote', 'args:'.print_r($err, true));
  			return false;
  		}
  		__kernel_system_debug::s('remote', 'no erorr');
  		if(isset($_SESSION['remote']['PHPSESSID'])) $requests = "?PHPSESSID={$_SESSION['remote']['PHPSESSID']}";
  		
  		$headers  = "GET /{$requests} HTTP/1.1\r\n";
  		$headers .= 'Host: '.$args->getVar('host')."\r\n";
	  	$headers .= "Remote: soul_remote_protocol\r\n";
	  	
	  	if($args->getVar('loginning') == true) {
      		$headers .= 'remote_login: '.$args->getVar('login')."\r\n";
	      	$headers .= 'remote_pass: '.$args->getVar('pass')."\r\n";
	  	} else {
	  	    $headers .= "remote_login: guest\r\n";
	  	}
  		$headers .= 'remote_class: '.$args->getVar('class')."\r\n";
  		$headers .= 'remote_method: '.$args->getVar('method')."\r\n";
  		$headers .= 'remote_args: '.serialize($args->getVar('args'))."\r\n";
	  	
  		$headers .= "Connection: Close\r\n\r\n";
  		
  		
  		fwrite($fp, $headers);
	  	$line = '';	
	  	while(!feof($fp)) {
	  		$line .= fgets($fp, 1024);
	  	}
	  		
	  	fclose($fp);
	  	
  		__kernel_system_debug::s('remote', 'response:'.$line);
	  	
	  	
	  	$line = explode("<!-- SERVER RESPONSE : BEGIN -->", $line);
	  	
	  	$explode = explode('Set-Cookie: PHPSESSID=', $line[0]);
	  	$explode = explode(';', $explode[1]);
	  	if($explode[0] != $_SESSION['remote']['PHPSESSID']) $_SESSION['remote']['PHPSESSID'] =  $explode[0];
	  	
	  	$explode = explode('remote_login: ', $line[0]);
	  	$explode = explode("\n", $explode[1]);
	  	if($explode[0] != 'ok')        $_SESSION['remote']['logged'] = true; 
	  	elseif($explode[0] != 'false') $_SESSION['remote']['logged'] = false;
	  	
	  	$return = unserialize( str_replace("<!-- SERVER RESPONSE : END -->", "", trim($line[1])) );
	  	
	  	if($return == $line[1]) return $line[1];
  		                   else return $return;
  		
  		
  	}
 	
 }
?>
